import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';

import 'package:casdon/modules/recipe/data/api/cook_module_content_api.dart';

import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';
import 'package:casdon/modules/recipe/providers/recipe_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CookModuleContentUsecase {
  final RecipeRepository _repository;

  const CookModuleContentUsecase(this._repository);

  Future<ResponseCookModuleContentApi> execute(
    RequestCookModuleContentApi request,
  ) async {
    try {
      var ret = await _repository.getCookModuleContent(request);
      ret ??= ResponseCookModuleContentApi(0, []);
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("CookModuleContentUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("CookModuleContentUsecase 失败");
    }
  }
}

final cookModuleContentUsecaseProvider = Provider(
  (ref) => CookModuleContentUsecase(ref.read(recipeRepository)),
);
